<?php


namespace app\admin\controller;


use app\common\lib\Auth;
use think\Db;
use think\Request;

class school extends Base
{
    public function index()
    {
        if(request()->isAjax()) {
            extract(input()); //将接收的参数赋值给变量
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $map = array();
            //关键字查询
            if(isset($keyword) && $keyword!="")
            {
                $map['name'] = ['like',"%" . $keyword . "%"];
            }
            try {
                $count = Db::name('school_year')->where($map)->count();  //总条目数据
                $lists = Db::name('school_year')->page($NowPage,$limits)->where($map)->select();  //当前页
            }catch (\Exception $e) {
                return json(['code' => 500, 'msg' => $e->getMessage()]);
            }
            return json(['code' => 200, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }

        /*************************检测权限 开始***********************/
        $auth = new Auth();
        $request = Request::instance();
        if(!$auth->check($request->path(),$this->isLogin()->id)) {
            return $this->redirect('not_rule/index/index');
        }
        /*************************检测权限 结束***********************/
        return $this->fetch();
    }

    public function add()
    {
        if (request()->isAjax()) {
            $data = input('post.');
            try{
                $_add_res = Model('SchoolYear')->save($data);
            }catch (\Exception $e) {
                return show('200',$e->getMessage(),'0');
            }
            if(!$_add_res) {
                return show('200','数据库错误','0');
            }
            return show('200','添加成功，刷新父页面生效','1');
        }else{
            return $this->fetch();
        }
    }

    public function edit()
    {
        if (\request()->isAjax()){
            $data = input('post.');
            try{
                $_edit_res = Model('SchoolYear')->save($data,['id'=>$data['id']]);
            }catch (\Exception $e) {
                return show('200',$e->getMessage(),'0');
            }
            if(!$_edit_res) {
                return show('200','数据库错误','0');
            }
            return show('200','修改成功，刷新父页面生效','1');
        }else {
            $id = Request::instance()->param('id');
            $this_data = Model('SchoolYear')->get(['id' => $id]);
            $this->assign([
                'this_data' => $this_data
            ]);
            return $this->fetch();
        }
    }

    public function del()
    {
        if(request()->isAjax()) {
            $id = Request::instance()->param('id');
            try{
                $res = Model('SchoolYear')->del_msg($id);
            }catch (\Exception $e){
                return show('200',$e->getMessage(),'0');
            }
            if (!$res) {
                return show('200','数据库错误','0');
            }
            return show('200','删除成功！','1');
        }else{
            return show('500','非法操作');
        }
    }
}